package cn.tedu.boot0701.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile pic) throws IOException {
        String fileName=pic.getOriginalFilename();
        System.out.println("原始名字:"+fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        System.out.println("唯一文件名:"+fileName);
        String dirName = "d:/files";
        File dirFile = new File(dirName);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        String path = dirName+"/"+fileName;
        pic.transferTo(new File(path));
        return "/"+fileName;
    }
}
